﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using DGSFramework;

public partial class Controls_PagingControl : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    private int mTotalPage;

    public int TotalPage
    {
        get { return mTotalPage; }
        set { mTotalPage = value; }
    }

    private string mpageUrl;

    public string pageUrl
    {
        get { return mpageUrl; }
        set { mpageUrl = value; }
    }

    private int mPageIndex;

    public int PageIndex
    {
        get { return mPageIndex; }
        set { mPageIndex = value; }
    }

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
    }

    protected override void Render(HtmlTextWriter writer)
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("<tr>");

        if (PageIndex > 1)
        {
            sb.Append("<td><a class='ptdv' href=\"" + (pageUrl.Contains(".aspx") == true ? pageUrl + "&page=1" : pageUrl.Substring(0, pageUrl.IndexOf(Globals.GetUrlRewriteExtension())) + "-trang-1" + Globals.GetUrlRewriteExtension()) + "\"> <i class='glyphicon glyphicon-fast-backward'></i> </a></td>");
            sb.Append("<td><a class='ptdv' href=\"" + (pageUrl.Contains(".aspx") == true ? pageUrl + "&page=" + (PageIndex - 1) : pageUrl.Substring(0, pageUrl.IndexOf(Globals.GetUrlRewriteExtension())) + "-trang-" + (PageIndex - 1) + Globals.GetUrlRewriteExtension()) + "\"> <i class='glyphicon glyphicon-step-backward'></i> </a></td>");
        }

        if (TotalPage <= 10)
        {
            for (int i = 1; i <= TotalPage; i++)
            {
                sb.Append("<td class='" + (mPageIndex == i ? "ptdvact" : "ptdv") + "'><a class='" + (mPageIndex == i ? "ptdvact " : "ptdv") + "'  href=\"" + (mPageIndex == i ? "#" : (pageUrl.Contains(".aspx") == true ? pageUrl + "&page=" + i : pageUrl.Substring(0, pageUrl.IndexOf(Globals.GetUrlRewriteExtension())) + "-trang-" + i + Globals.GetUrlRewriteExtension())) + "\">" + i + "</a></td>");
            }
        }
        else
        {
            if (PageIndex <= 5)
            {
                for (int i = 1; i < PageIndex; i++)
                {
                    sb.Append("<td class='" + (mPageIndex == i ? "ptdvact" : "ptdv") + "'><a class='" + (mPageIndex == i ? "ptdvact " : "ptdv") + "' href=\"" + (mPageIndex == i ? "#" : (pageUrl.Contains(".aspx") == true ? pageUrl + "&page=" + i : pageUrl.Substring(0, pageUrl.IndexOf(Globals.GetUrlRewriteExtension())) + "-trang-" + i + Globals.GetUrlRewriteExtension())) + "\">" + i + "</a></td>");
                }
            }
            if (PageIndex - 5 >= 1)
            {
                //sb.Append("<a href='#'>< Prev</a>");
                for (int i = PageIndex - 4; i < PageIndex; i++)
                {
                    sb.Append("<td class='" + (mPageIndex == i ? "ptdvact" : "ptdv") + "'><a class='" + (mPageIndex == i ? "ptdvact " : "ptdv") + "' href=\"" + (mPageIndex == i ? "#" : (pageUrl.Contains(".aspx") == true ? pageUrl + "&page=" + i : pageUrl.Substring(0, pageUrl.IndexOf(Globals.GetUrlRewriteExtension())) + "-trang-" + i + Globals.GetUrlRewriteExtension())) + "\">" + i + "</a></td>");
                }
            }

            if (PageIndex + 5 < TotalPage)
            {
                for (int i = PageIndex; i < PageIndex + 5; i++)
                {
                    sb.Append("<td class='" + (mPageIndex == i ? "ptdvact" : "ptdv") + "'><a class='" + (mPageIndex == i ? "ptdvact " : "ptdv") + "' href=\"" + (mPageIndex == i ? "#" : (pageUrl.Contains(".aspx") == true ? pageUrl + "&page=" + i : pageUrl.Substring(0, pageUrl.IndexOf(Globals.GetUrlRewriteExtension())) + "-trang-" + i + Globals.GetUrlRewriteExtension())) + "\">" + i + "</a></td>");
                }
                //sb.Append("<a href='#'>Next ></a>");
            }
            else if (PageIndex + 5 >= TotalPage)
            {
                for (int i = PageIndex; i <= TotalPage; i++)
                {
                    sb.Append("<td class='" + (mPageIndex == i ? "ptdvact" : "ptdv") + "'><a class='" + (mPageIndex == i ? "ptdvact " : "ptdv") + "' href=\"" + (mPageIndex == i ? "#" : (pageUrl.Contains(".aspx") == true ? pageUrl + "&page=" + i : pageUrl.Substring(0, pageUrl.IndexOf(Globals.GetUrlRewriteExtension())) + "-trang-" + i + Globals.GetUrlRewriteExtension())) + "\">" + i + "</a></td>");
                }
            }
        }

        if (PageIndex < TotalPage)
        {
            sb.Append("<td><a class='ptdv' href=\"" + (pageUrl.Contains(".aspx") == true ? pageUrl + "&page=" + (PageIndex + 1) : pageUrl.Substring(0, pageUrl.IndexOf(Globals.GetUrlRewriteExtension())) + "-trang-" + (PageIndex + 1) + Globals.GetUrlRewriteExtension()) + "\"> <i class='glyphicon glyphicon-step-forward'></i> </a></td>");
            sb.Append("<td><a class='ptdv' href=\"" + (pageUrl.Contains(".aspx") == true ? pageUrl + "&page=" + (TotalPage) : pageUrl.Substring(0, pageUrl.IndexOf(Globals.GetUrlRewriteExtension())) + "-trang-" + (TotalPage) + Globals.GetUrlRewriteExtension()) + "\"> <i class='glyphicon glyphicon-fast-forward'></i> </a></td>");
        }

        sb.Append("</tr>");
        writer.Write(sb);
        base.Render(writer);
    }
}